package cyh.view;

import cyh.domain.dto.DeptDto;
import cyh.domain.vo.DeptVo;
import cyh.domain.vo.Result;
import cyh.service.IDeptService;
import cyh.service.array.DeptService;

public class DeptView extends BaseView<DeptVo,IDeptService> {
    /**
     * 视图对象缓存数组
     */
    private static final String MENU = """
            ----------------
                 部门管理
            ----------------
            | 0 返回上级     |
            | 1 部门列表     |
            | 2 创建部门     |
            | 3 更新部门     |
            | 4 删除部门     |
            ----------------
            请输入选项：""";
    private static DeptView singleton;

    private DeptView() {
        service = DeptService.getInstance();
    }

    public static DeptView getInstance() {
        if (singleton == null) {
            singleton = new DeptView();
        }
        return singleton;
    }

    public void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("以返回上一级菜单!");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> delete();
                default -> System.out.println("输入错误，请重新输入!");
            }
        } while (!"0".equals(menuChoice));
    }


    public void list() {
        Result result = service.list();
        cache = (DeptVo[]) result.data;
        System.out.println("ID\t部门名称\t部门电话");
        for (DeptVo vo : cache) {
            System.out.printf("%d\t%s\t%s\n", vo.getId(), vo.name, vo.phone);
        }
    }

    public void create() {
        System.out.println("请输入部门名称");
        String name = MainView.input.nextLine();
        System.out.println("请输入部门电话");
        String phone = MainView.input.nextLine();
        DeptDto dto = new DeptDto(name, phone);
        Result result = service.create(dto);
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
    }

    public void update() {
        DeptVo dept = getCacheById();
        if (dept == null) {
            printError("没有找到对应的数据，请重新输入!");
            return;
        }
        System.out.println("请输入部门名称,直接按回车表示不修改");
        String name = MainView.input.nextLine();
        name = "".equals(name) ? dept.name : name;
        System.out.println("请输入部门电话,直接按回车表示不修改");
        String phone = MainView.input.nextLine();
        phone = "".equals(phone) ? dept.phone : phone;
        DeptDto dto = new DeptDto(name, phone);
        Result result = service.update(dto, dept.getId());
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();

    }

    public void delete() {
        DeptVo dept = getCacheById();
        System.out.println("确认删除输入Y");
        String confirm = MainView.input.nextLine();
        if (!"Y".equalsIgnoreCase(confirm)) {
            System.out.println("已取消删除!");
            return;
        }
        Result result = service.delete(dept.getId());
        if (!result.success) {
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


}